package cn.study.reentrantlock;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author 跟大佳一起学架构
 * <p>
 * 根据QMode的不同，会执行不同的唤醒策略
 */
@Slf4j
public class LockQModeDemo {

    public static void main(String[] args) throws InterruptedException {

        LockQModeDemo demo = new LockQModeDemo();

        demo.startThreadA();
        //控制线程执行时间
        Thread.sleep(1000);
        demo.startThreadB();
        Thread.sleep(1000);
        demo.startThreadC();
    }
    private final ReentrantLock lock = new ReentrantLock(false); // ...

    public void startThreadA() {
        new Thread(() -> {
            lock.lock();
            log.debug("A get lock");
            try {
                Thread.sleep(3000);
//                lock.wait(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.debug("A release lock");
            lock.unlock();

        }, "thread-A").start();
    }

    public void startThreadB() {
        new Thread(() -> {
            lock.lock();
            log.debug("B get lock");
            lock.unlock();
        }, "thread-B").start();
    }

    public void startThreadC() {
        new Thread(() -> {
            lock.lock();
            log.debug("C get lock");
            lock.unlock();
        }, "thread-C").start();
    }

}
